//
//  SelectAndShowPhotoView.m
//  ImGuider
//
//  Created by 李陆涛 on 2017/10/13.
//  Copyright © 2017年 AudioCtrip. All rights reserved.
//

#import "SelectAndShowPhotoView.h"
#import "ShowPhotoCell.h"
#import "SDPhotoBrowser.h"
#import "ChoosePhotoVC.h"

@interface SelectAndShowPhotoView () <UICollectionViewDataSource, UICollectionViewDelegate, SDPhotoBrowserDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation SelectAndShowPhotoView


- (NSMutableArray *)dataSource {
    
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    
    return _dataSource;
}

- (void)awakeFromNib {
    
    [super awakeFromNib];
    [self setupUI];
}

- (instancetype)init {
    
    self = [super init];
    if (self) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    self.userInteractionEnabled = YES;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    _collectionView.scrollEnabled = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_collectionView];
    
    [_collectionView registerClass:[ShowPhotoCell class] forCellWithReuseIdentifier:@"ShowPhotoCell"];
    
    @weakify(self);
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.left.equalTo(self);
        make.top.equalTo(self);
        make.bottom.equalTo(self);
        
        make.width.mas_equalTo(76 * 3 + 10 + 10);
        
    }];
}

#pragma mark UICollectionView

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (self.maxCount > self.dataSource.count) {
        
        return self.dataSource.count + 1;
        
    }
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ShowPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShowPhotoCell" forIndexPath:indexPath];
    
    if (indexPath.row == self.dataSource.count) {
        
        cell.imageView.image = [UIImage imageNamed:@"addPhoto"];
        cell.deleteBtn.hidden = YES;
        
        cell.imageView.contentMode = UIViewContentModeCenter;
    } else {
        cell.imageView.image = self.dataSource[indexPath.row];
        cell.deleteBtn.hidden = NO;
        
        cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    
    cell.deleteBtn.tag = indexPath.row;
    
    [cell.deleteBtn addTarget:self action:@selector(deleteBtnClik:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == self.dataSource.count) {
        
        ChoosePhotoVC *chooseVC = [ChoosePhotoVC alertControllerWithTitle:nil message:MYLocalizedString(@"MYINFO_SELECT_PHOTO", nil) preferredStyle:UIAlertControllerStyleActionSheet];
        chooseVC.maxCount = self.maxCount - self.dataSource.count;
        
        @weakify(self);
        [chooseVC showWithViewController:self.superVC selectArrayBlock:^(NSArray *photoArray) {
            
            @strongify(self);
            [self.dataSource addObjectsFromArray:photoArray];
            [self.collectionView reloadData];
        }];
        
    } else { // preview photos or video / 预览照片或者视频
        
        SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
        
        browser.sourceImagesContainerView = collectionView;
        browser.currentImageIndex = indexPath.row;
        browser.imageCount = self.dataSource.count;
        
        browser.delegate = self;
        browser.type = 1;
        [browser show];
    }
}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
    
    return self.dataSource[index];
}
- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
    
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(76, 76);
}
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    
    return 10;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    
    return 0;
}

- (void)deleteBtnClik:(UIButton *)sender {
    
    [self.dataSource removeObjectAtIndex:sender.tag];
    
    [_collectionView reloadData];
}

@end

